package com.ruoyi.project.zxcrm.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.zxbd.service.impl.BdPartnerServiceImpl;
import com.ruoyi.project.zxcrm.domain.CrmResume;
import com.ruoyi.project.zxcrm.mapper.CrmResumeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 简历信息Service业务层处理
 *
 * @author mn
 * @date 2022-05-13
 */
@Service
public class CrmResumeServiceImpl extends BaseController {
    @Autowired
    private CrmResumeMapper crmResumeMapper;
    @Autowired
    private BdPartnerServiceImpl BdPartnerService;
    @Autowired
    private CrmLeadServiceImpl CrmLeadService;
    /**
     * 查询简历信息
     *
     * @param id 简历信息主键
     * @return 简历信息
     */

    public CrmResume selectCrmResumeById(Long id) {
        return crmResumeMapper.selectCrmResumeById(id);
    }

    /**
     * 查询简历信息列表
     *
     * @param crmResume 简历信息
     * @return 简历信息
     */

    public List<CrmResume> selectCrmResumeList(CrmResume crmResume) {

//        // suwr多租用户信息条件2023年1月24日, PM 04:45:57
//        Long tenanId = SecurityUtils.getUserTenantId();
//        crmResume.setTenantId(tenanId);
        return crmResumeMapper.selectCrmResumeList(crmResume);
    }

    /**
     * 新增简历信息
     *
     * @param crmResume 简历信息
     * @return 结果
     */

    public int insertCrmResume(CrmResume crmResume) {

//        // suwr多租用户信息条件2023年1月24日, PM 04:45:57
//        Long tenanId = SecurityUtils.getUserTenantId();
        crmResume.setTenantId(this.getUserTenantId());
        crmResume.setUserId(this.getUserId());
        crmResume.setCreateBy(this.getUsername());
        crmResume.setUpdateBy(this.getUsername());
        crmResume.setCreateTime(DateUtils.getNowDate());
        crmResume.setUpdateTime(DateUtils.getNowDate());
        if (crmResume.getParams("is_partner_update").equals("0")){
            this.updatePartner(crmResume);
        }
        if (crmResume.getParams("is_lead_update").equals("0")){
            this.updateLead(crmResume);
        }
        return crmResumeMapper.insertCrmResume(crmResume);
    }

    /**
     * 修改简历信息
     *
     * @param crmResume 简历信息
     * @return 结果
     */

    public int updateCrmResume(CrmResume crmResume) {
        crmResume.setTenantId(this.getUserTenantId());
        crmResume.setUpdateBy(this.getUsername());
        crmResume.setUpdateTime(DateUtils.getNowDate());
        if (crmResume.getParams("is_partner_update").equals("0")){
            this.updatePartner(crmResume);
        }
        if (crmResume.getParams("is_lead_update").equals("0")){
            this.updateLead(crmResume);
        }
        return crmResumeMapper.updateCrmResume(crmResume);
    }

    /**
     * 批量删除简历信息
     *
     * @param ids 需要删除的简历信息主键
     * @return 结果
     */

    public int deleteCrmResumeByIds(Long[] ids) {
        return crmResumeMapper.deleteCrmResumeByIds(ids);
    }

    /**
     * 删除简历信息信息
     *
     * @param id 简历信息主键
     * @return 结果
     */

    public int deleteCrmResumeById(Long id) {
        return crmResumeMapper.deleteCrmResumeById(id);
    }


    public CrmResume checkResumePhone(CrmResume resume) {
        List<CrmResume> crmResumes = crmResumeMapper.checkResumePhone(resume);
        if (crmResumes.isEmpty()) {
            return null;
        }
        return crmResumes.get(0);
    }

    public void updateCrmResumeByMobile(CrmResume crmResume) {
        crmResumeMapper.updateCrmResumeByMobile(crmResume);
    }

    public void updatePartner(CrmResume resume) {

//      BdPartner BdPartner = JSONObject.parseObject(jsonStr, BdPartner.class);
//		BdPartner.setSales(getUserId());
        PageData pageData = new PageData();
        pageData.put("mobile", resume.getMobile());
        List<PageData> list = BdPartnerService.selectBdPartnerListPd(pageData);
        PageData existsPartner;

        if (list.size() > 0) {
            existsPartner = list.get(0);
            existsPartner.put("remark", existsPartner.get("remark") + ",\n" + DateUtils.getDate() + ",再次投递,");
            existsPartner.put("name", resume.getName());
            existsPartner.put("sex", resume.getSex());
            existsPartner.put("enable_flag", "0");
            existsPartner.put("is_customer", "0");
            BdPartnerService.updateBdPartnerPd(existsPartner);
        } else {
            existsPartner = new PageData();
            existsPartner.put("mobile", resume.getMobile());
            existsPartner.put("name", resume.getName());
            existsPartner.put("sex", resume.getSex());
            existsPartner.put("user_id", this.getUserId());
            existsPartner.put("enable_flag", "0");
            existsPartner.put("is_customer", "0");
            existsPartner.put("remark", DateUtils.getDate() + ",初次导入,");
            BdPartnerService.insertBdPartnerPd(existsPartner);
        }
    }

    public AjaxResult updateLead(CrmResume resume) {

        PageData partner = new PageData();
        partner.put("mobile",resume.getMobile());
        List<PageData> list = BdPartnerService.selectBdPartnerListPd(partner);
        PageData existsPartner;
        if (list.size() > 0) {
            existsPartner = list.get(0);
        }else {
            existsPartner = null;
            return AjaxResult.error("联系人表中未存在，创建跟单数据失败！！");
        }

        PageData pdLead = CrmLeadService.selectCrmLeadById(existsPartner.getLong("id"));
        if (pdLead == null){
            pdLead = new PageData();
            pdLead.put("id",existsPartner.getLong("id"));
            pdLead.put("source",resume.getSource());
            CrmLeadService.insertCrmLead(pdLead);
        }else {
            pdLead.put("next_time",DateUtils.getNowDate());
            pdLead.put("remark",pdLead.getString("remark") + ",\n" + DateUtils.getDate() + ",再次投递");
            CrmLeadService.updateCrmLead(pdLead);
        }
        return null;
    }

}
